package com.huabo.contract.controller;

import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.alibaba.fastjson.JSONObject;
import com.hbfk.util.FtpUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.contract.oracle.entity.TblAttachment;
import com.huabo.contract.oracle.entity.TblContractProject;
import com.huabo.contract.oracle.vo.TblContractProjectVo;
import com.huabo.contract.service.TblAttachmentService;
import com.huabo.contract.service.TblContractProjectService;
import com.huabo.contract.service.TblLegalDisputregistrationService;
import com.huabo.contract.service.TblOrganizaService;
import com.huabo.contract.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 项目管理
 */
@RestController
@Slf4j
@Api(value = "项目管理", tags = { "项目管理接口" })
@RequestMapping(value = "/contractPro")
public class ContractProjectController {
	
	@Resource
	public TblContractProjectService tblContractProjectService;
	
	@Resource
	private TblAttachmentService tblAttachmentService;
	
	@Resource
	private TblLegalDisputregistrationService tblLegalDisputregistrationService;
	
	@Resource
	public UserService userService;
	
	@Resource
	public TblOrganizaService tblOrganizaService;
	
	
	/**
	 * 立项管理列表
	 */
	@GetMapping("/htlx/contract_pro_list")
	@ApiOperation(value = "立项管理列表")
	public JsonBean contract_pro_list(HttpServletRequest request, TblContractProjectVo tblContractProjectVo,
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize) {

		JsonBean jsonBean = null;
		try {
			jsonBean = tblContractProjectService.cpPageList(token, pageNumber, pageSize,tblContractProjectVo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
	/**
	 * 立项管理-新增与修改
	 */
	@RequestMapping(value = "/htlx/contract_pro_save", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "立项管理-新增与修改")
    public JsonBean contract_pro_save(HttpServletRequest request,@ApiParam(name="cp",value="实体",required=true)TblContractProject cp,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="attIds",value="附件主键拼接英文逗号分割 示例：1,2,3,4",required=false)@RequestParam(value = "attIds", required = false) String attIds)throws Exception{
		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblContractProjectService.cpAdd(cp,token,attIds);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
	
	/**
	 * 立项管理-新增与修改
	 */
	@RequestMapping(value = "/htlx/contract_pro_delFile", produces = "application/json; charset=utf-8", method = {RequestMethod.GET})
	@ApiOperation(value = "立项管理-删除附件")
    public JsonBean contract_pro_delFile(HttpServletRequest request,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token,
			 @ApiParam(name="attId",value="删除的附件Id",required=false)@RequestParam(value = "attId", required = false) String attId)throws Exception{
		JsonBean jsonBean = null;
		try {
			jsonBean = this.tblContractProjectService.removeFile(token,attId);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
	
	/**
	 * 立项管理-删除
	 */
	@GetMapping("/htlx/contract_pro_del")
	@ApiOperation(value = "立项管理-删除")
    public JsonBean contract_pro_del(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="projectid",value="主键",required=true)@RequestParam(value = "projectid", required = true) Integer projectid) {
        
        try {
			return tblContractProjectService.cpDelete(projectid, token);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return null;
    }
	/**
	 * 立项管理-明细
	 */
	@GetMapping("/htlx/contract_pro_detail")
    @ApiOperation(value = "立项管理-明细")
    public JsonBean contract_pro_detail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="projectid",value="主键",required=true)@RequestParam(value = "projectid", required = true) Integer projectid) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblContractProjectService.findContractProjectDetail(token,projectid);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
	
	/**
	 * 立项管理-上传附件接口
	 */
	@RequestMapping(value = "/uploadFileAttInfo", produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ApiOperation(value="立项管理-上传附件接口(type=5)")
	public String uploadFileAttInfo(HttpServletRequest request, HttpServletResponse response, Model map,MultipartFile file,
			@RequestParam(value="bid",required=false)Integer bid,
			@RequestParam(value="type",required=false)Integer type,
			@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token") String token,@ApiParam(name="staffId",value="登录用户主键",required=false)String staffId){
		String result = null;
		BigDecimal aid = null;
		// 创建一个通用的多部分解析器
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		// 判断 request 是否有文件上传,即多部分请求
		try {
			if (multipartResolver.isMultipart(request)) {
				// 转换成多部分request
				 MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
				 Map<String,Object> resultMap =  this.tblAttachmentService.uploadAttachment(multiRequest,token,staffId,file);
				 JSONObject jsonObj = new JSONObject(resultMap);
				 result = jsonObj.toString();
				 if(bid != null && type != null){
					 TblAttachment tblAttachment = (TblAttachment)resultMap.get("data");
					 aid = tblAttachment.getAttid();
					 tblLegalDisputregistrationService.saveBidType(type,bid,aid);
				 }

            }

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping(value = "/deleteFileRelation",produces = "application/json; charset=utf-8",method = {RequestMethod.POST})
	@ApiOperation("删除附件")
	public @ResponseBody String deleteFileRelation(HttpServletRequest request,
						@RequestParam(value = "attid",required = false)String attid){

			tblLegalDisputregistrationService.deleteRelation(attid);
			//TblAttachment att = tblAttachmentService.findById(attid);
			tblAttachmentService.delete(attid);
			return JsonBean.success();
	}
	@Value("${file.path}")
	private String fileUrl;
	
	/**
	 * 文件下载
	 */
	@ApiOperation(value = "文件下载", httpMethod = "POST", produces = "application/json")
	@RequestMapping(value = "/download", produces = "application/json; charset=utf-8",method = {RequestMethod.GET})
	public void fileDownLoad(@RequestParam("id") String id, HttpServletResponse httpServletResponse) {
		TblAttachment tblAttachmentEntity = tblAttachmentService.findById(id);
		FtpUtil.downUploadFile(tblAttachmentEntity,httpServletResponse);
		//return JsonBean.success();
	}
	
	/**
     * 选择人员列表
     */
    @GetMapping("/user/user_list")
	@ApiOperation(value = "选择人员列表")
	public JsonBean user_list(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "pageNumber", value = "分页当前页数", required = false) @RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
			@ApiParam(name = "pageSize", value = "每页记录数", required = false) @RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
			@ApiParam(name = "orgid", value = "部门id", required = false) @RequestParam(value = "orgid", required = false) Integer orgid) {

		JsonBean jsonBean = null;
		try {
			jsonBean = userService.findAllPageBeanPid(token, pageNumber, pageSize,orgid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
    
    /**
     * 选择部门列表
     */
    @GetMapping("/user/org_list")
	@ApiOperation(value = "选择部门列表")
	public JsonBean org_list(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token) {

		JsonBean jsonBean = null;
		try {
			jsonBean = tblOrganizaService.getOrgTreeListByAuditObj(token, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
    
    /**
     * 项目立项-附件列表
     */
    @GetMapping("/htlx/file_list")
	@ApiOperation(value = "项目立项-附件列表")
	public JsonBean user_list(HttpServletRequest request, 
			@ApiParam(name = "token", value = "登录用户token", required = true) @RequestHeader("token") String token,
			@ApiParam(name = "projectid", value = "项目id(projectid)", required = true) @RequestParam(value = "projectid", required = true) Integer projectid) {

		JsonBean jsonBean = null;
		try {
			jsonBean = tblAttachmentService.htlxFileList(token,projectid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonBean;
	}
    
    /**
     * 
     */
	
}
